package org.happen.android.ui.widget;

import org.happen.android.R;
import org.happen.android.base.BaseLvAdapter;
import org.happen.android.widget.PopupDialog.BasePopupDialogWidget;
import org.happen.android.widget.PopupDialog.OnPopupDialogWidgetCallback;
import org.liushui.mycommons.android.annotation.ViewInject;
import org.liushui.mycommons.android.annotation.helper.InjectHelper;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

public class SelectListWidget extends BasePopupDialogWidget
{
	@ViewInject(R.id.listView1)
	ListView listView1;

	SelectArgs searchArgs;
	String[] data = { "附近", "热门商区", "徐汇区", "松江区" };
	OnSelectCallback onSelectListCallback = new OnSelectCallback() {
		public void onCallback(SelectArgs args)
		{

		}
	};
	BaseLvAdapter adapter1;

	public SelectListWidget(Context context, SelectArgs args)
	{
		super(context);
		this.searchArgs = args;
	}

	public void init()
	{
		View.inflate(context, R.layout.view_shangquan_search_sort, this);
		InjectHelper.init(this, this);

		adapter1 = new LvAdapter1();
		listView1.setAdapter(adapter1);
		listView1.setOnItemClickListener(adapter1);
	}

	public void setData(String[] data)
	{
		this.data = data;
	}

	class LvAdapter1 extends BaseLvAdapter
	{

		public int getCount()
		{
			return data.length;
		}

		public String getItem(int position)
		{
			return data[position];
		}

		public View getView(int position, View convertView, ViewGroup parent)
		{
			SelectViewHolder vh = null;
			if (convertView == null)
			{
				convertView = View.inflate(context, R.layout.item_sq_search_town2, null);
				vh = new SelectViewHolder();
				InjectHelper.init(vh, convertView);
				convertView.setTag(vh);
			} else
			{
				vh = (SelectViewHolder) convertView.getTag();
			}
			vh.text.setText(getItem(position));

			if (position == searchArgs.index)
			{
				vh.text.setTextColor(getResources().getColor(R.color.app_theme_color2));
				vh.line.setBackgroundResource(R.drawable.dotted_line_p);
			} else
			{
				vh.text.setTextColor(getResources().getColor(R.color.black));
				vh.line.setBackgroundResource(R.drawable.dotted_line);
			}
			return convertView;
		}

		public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
		{
			searchArgs.index = position;
			searchArgs.obj = getItem(position);
			onSelectListCallback.onCallback(searchArgs);
			popupDialog.dismiss();
		}
	}

	public void setOnSelectListCallback(OnSelectCallback onSelectListCallback)
	{
		this.onSelectListCallback = onSelectListCallback;
	}

	public interface OnSelectCallback extends OnPopupDialogWidgetCallback
	{

		void onCallback(SelectArgs args);
	}
}
